package com.vict.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class Screenshot {

	public static void printImg(int x, int y, int width, int height, File file) {
		try{
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			Rectangle screenRectangle = new Rectangle(screenSize);
			Robot robot = new Robot();
			BufferedImage image = robot.createScreenCapture(screenRectangle);
			// 截图保存的路径
			// 如果文件夹路径不存在，则创建
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}

			// 指定屏幕区域，参数为截图左上角坐标(100,100)+右下角坐标(500,500)
			BufferedImage subimage = image.getSubimage(x, y, width, height);
			ImageIO.write(subimage, "png", file);
		}catch(Exception e){
			throw new RuntimeException(e);
		}

	}

	public static void printImgXY(int x, int y, int x1, int y1, File file) {
		printImg(x, y, x1 - x, y1 - y, file);
	}
}
